import axios from "axios"
import {
    Message
} from "element-ui"
import router from "../router/index"
axios.defaults.baseURL = "/api"

axios.interceptors.request.use(
    function (request) {
        const storage = JSON.parse(localStorage.getItem("loginData" || '{}'))
        if (storage) {
            request.headers.Authorization = storage.token
        }
        return request
    } //拦截器功能函数，自动接受请求，必须返回请求
) //请求拦截器


axios.interceptors.response.use(
    function (response) {
        //响应如果遇到token过期，要求从新登录 403
        if (response.data.code == 403) {
            Message.error(response.data.msg)
            router.push("/login")
        }
        return response.data // 根据axios接口惯例，过滤多余的内容
    }
)
export default axios